package com.hopes.bizz.risk.tenant.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hopes.bizz.risk.setting.entity.RiskConfigLEntity;
import com.hopes.bizz.risk.setting.entity.RiskConfigLecEntity;
import com.hopes.bizz.risk.setting.entity.RiskConfigLsEntity;
import com.hopes.bizz.risk.setting.entity.RiskConfigScEntity;
import com.hopes.bizz.risk.setting.service.RiskConfigLService;
import com.hopes.bizz.risk.setting.service.RiskConfigLecService;
import com.hopes.bizz.risk.setting.service.RiskConfigLsService;
import com.hopes.bizz.risk.setting.service.RiskConfigScService;
import com.hopes.bizz.risk.tenant.service.TenantConfigInitService;
import com.hopes.boss.admin.api.entity.SysTenant;
import com.hopes.boss.common.data.tenant.TenantContextHolder;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author daijie
 * @version 1.0
 * @date 2023/8/14 14:04
 * @Description:
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class RiskConfigTenantInitServiceImpl implements TenantConfigInitService {

    private final RiskConfigLService riskConfigLService;
    private final RiskConfigLecService riskConfigLecService;
    private final RiskConfigLsService riskConfigLsService;
    private final RiskConfigScService riskConfigScService;

    @Override
    public String getName() {
        return "初始化评估方法设置";
    }

    @Override
    public void init(SysTenant tenant) {
        Long tenantId = tenant.getId();

        List<RiskConfigLEntity> configLList = riskConfigLService.list(Wrappers.<RiskConfigLEntity>lambdaQuery().eq(RiskConfigLEntity::getTenantId, TenantContextHolder.getAdminTenantId()));
        for (RiskConfigLEntity entity : configLList) {
            entity.setId(null);
            entity.setTenantId(tenantId);
        }
        riskConfigLService.saveBatch(configLList);

        List<RiskConfigLecEntity> configLecList = riskConfigLecService.list(Wrappers.<RiskConfigLecEntity>lambdaQuery().eq(RiskConfigLecEntity::getTenantId, TenantContextHolder.getAdminTenantId()));
        for (RiskConfigLecEntity entity : configLecList) {
            entity.setId(null);
            entity.setTenantId(tenantId);
        }
        riskConfigLecService.saveBatch(configLecList);

        List<RiskConfigLsEntity> configLsList = riskConfigLsService.list(Wrappers.<RiskConfigLsEntity>lambdaQuery().eq(RiskConfigLsEntity::getTenantId, TenantContextHolder.getAdminTenantId()));
        for (RiskConfigLsEntity entity : configLsList) {
            entity.setId(null);
            entity.setTenantId(tenantId);
        }
        riskConfigLsService.saveBatch(configLsList);

        List<RiskConfigScEntity> configScList = riskConfigScService.list(Wrappers.<RiskConfigScEntity>lambdaQuery().eq(RiskConfigScEntity::getTenantId, TenantContextHolder.getAdminTenantId()));
        for (RiskConfigScEntity entity : configScList) {
            entity.setId(null);
            entity.setTenantId(tenantId);
        }
        riskConfigScService.saveBatch(configScList);
    }
}
